//
//  AppDelegate.swift
//  weibo10
//
//  Created by mac on 15/10/16.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        setupAppearance()
    
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController
        window?.makeKeyAndVisible()
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, queue: nil) {[weak self](notification) -> Void in
            print(notification.object)
            
            self?.window?.rootViewController = notification.object == nil ?HMMainController() : HMWelcomController()
        }
        return true
    }
    // 注销通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WBSwitchRootViewControllerNotification, object: nil)
    }
    
    // 全局外观
    private func setupAppearance(){
        UINavigationBar.appearance().tintColor = WbAppearanceTintColor
        UITabBar.appearance().tintColor = WbAppearanceTintColor
    }
}
// MARK: 界面切换
extension AppDelegate{
    /// 需要跳转的控制器
    private var defaultRootViewController: UIViewController{
        if HMUserAcountViewModel.sharedUserAccount.userLogon {
            return isNewVersion ? HMNewFeatureController() : HMWelcomController()
        }
        return HMMainController()
    
    }
    /// 判断版本
    private var isNewVersion: Bool{
        // 获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let version = Double(currentVersion)!
        
        // 获取之前版本
        let sandBoxVersionKey = "sandBoxVersionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxVersionKey)
        
        // 存储当前版本
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandBoxVersionKey)
        // 判断版本
        return version > sandBoxVersion
    }
}

